package com.ruoyi.config;

import com.ruoyi.domain.TtsAx;
import com.ruoyi.domain.TtsEventLog;
import com.ruoyi.mapper.TtsEventLogMapper;
import com.ruoyi.resp.AlarmNotification;
import com.ruoyi.system.domain.TtsCameraInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class AlarmNotificationService {

    private final SimpMessagingTemplate messagingTemplate;
    private final TtsEventLogMapper alarmMapper;

    public AlarmNotificationService(SimpMessagingTemplate messagingTemplate,
                                    TtsEventLogMapper alarmMapper) {
        this.messagingTemplate = messagingTemplate;
        this.alarmMapper = alarmMapper;
    }

    public void processNewAlarm(TtsEventLog event) {
        //返回结果
        ArrayList<AlarmNotification> objects = new ArrayList<>();
        //判断code
        if (event.getCode() != null) {
            //第一种如果code等于255
            if (event.getCode() == 255) {
                // 构建通知对象
                AlarmNotification notification = new AlarmNotification();
                notification.setId(String.valueOf(event.getId()));
                notification.setEventTime(String.valueOf(event.getEventTime()));
                notification.setContent(event.getContent());
                notification.setCameraId("");
                notification.setCameraName("");
                notification.setCameraCode("");
                notification.setStatusCode(String.valueOf(event.getStatusCode()));
                notification.setRtspUrl("");
                objects.add(notification);
            } else {
                //第二种如果code不等于255
                //获取关联的消防通道
                TtsAx ax = alarmMapper.findAxByCode(event.getCode());
                //如果ax存在并且bid不为空
                if (ax != null && ax.getBId() != null) {
                    //根据ax的bid去查询摄像头，其中ax以，逗号分割实现
                    String[] split = ax.getBId().split(",");
                    for (String id : split) {
                        TtsCameraInfo camera = alarmMapper.findCameraByDeviceId(Long.parseLong(id));
                        if (camera != null) {
                            // 构建通知对象
                            AlarmNotification notification = new AlarmNotification();
                            notification.setId(String.valueOf(event.getId()));
                            notification.setEventTime(String.valueOf(event.getEventTime()));
                            notification.setContent(event.getContent());
                            notification.setCameraId(String.valueOf(camera.getId()));
                            notification.setCameraName(camera.getName());
                            notification.setCameraCode(camera.getIndexCode());
                            notification.setStatusCode(String.valueOf(event.getStatusCode()));
                            notification.setRtspUrl(camera.getRtsp());
                            objects.add(notification);
                        }
                    }
                }else if (ax != null){
                    //如果ax存在但是bid不存在构建通知对象
                    AlarmNotification notification = new AlarmNotification();
                    notification.setId(String.valueOf(event.getId()));
                    notification.setEventTime(String.valueOf(event.getEventTime()));
                    notification.setContent(event.getContent());
                    notification.setCameraId("");
                    notification.setCameraName("");
                    notification.setCameraCode("");
                    notification.setStatusCode(String.valueOf(event.getStatusCode()));
                    notification.setRtspUrl("");
                    objects.add(notification);
                }
            }
            System.out.println("发送消息：" + objects);
            // 发送WebSocket通知
            messagingTemplate.convertAndSend("/topic/alarms", objects);
        }

    }
}